package com.ruoyi.activity.service.impl;

import java.util.List;

import com.ruoyi.activity.domain.WorksDetail;
import com.ruoyi.activity.mapper.WorksDetailMapper;
import com.ruoyi.activity.req.ReviewWorkReq;
import com.ruoyi.activity.req.WorksListReq;
import com.ruoyi.activity.req.WorksSignReq;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.activity.mapper.WorksMapper;
import com.ruoyi.activity.domain.Works;
import com.ruoyi.activity.service.IWorksService;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

/**
 * 作品管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-30
 */
@Service
public class WorksServiceImpl implements IWorksService
{
    @Autowired
    private WorksMapper worksMapper;
    @Resource
    private WorksDetailMapper worksDetailMapper;

    /**
     * 查询作品管理
     *
     * @param id 作品管理主键
     * @return 作品管理
     */
    @Override
    public Works selectWorksById(Long id)
    {
        return worksMapper.selectWorksById(id);
    }

    /**
     * 查询作品管理列表
     *
     * @param works 作品管理
     * @return 作品管理
     */
    @Override
    public List<Works> selectWorksList(WorksListReq works)
    {
        return worksMapper.selectWorksList(works);
    }

    /**
     * 新增作品管理
     *
     * @param works 作品管理
     * @return 结果
     */
    @Override
    public int insertWorks(Works works)
    {
        works.setCreateTime(DateUtils.getNowDate());
        return worksMapper.insertWorks(works);
    }

    /**
     * 修改作品管理
     *
     * @param works 作品管理
     * @return 结果
     */
    @Override
    public int updateWorks(Works works)
    {
        works.setUpdateTime(DateUtils.getNowDate());
        return worksMapper.updateWorks(works);
    }

    /**
     * 批量删除作品管理
     *
     * @param ids 需要删除的作品管理主键
     * @return 结果
     */
    @Override
    public int deleteWorksByIds(Long[] ids)
    {
        return worksMapper.deleteWorksByIds(ids);
    }

    /**
     * 删除作品管理信息
     *
     * @param id 作品管理主键
     * @return 结果
     */
    @Override
    public int deleteWorksById(Long id)
    {
        return worksMapper.deleteWorksById(id);
    }

    @Override
    public String sign(WorksSignReq worksSignReq) {
        if(worksSignReq.getId() == null){
            //插入作品
            Works works = new Works();
            BeanUtils.copyProperties(worksSignReq,works);
            works.setCreateTime(DateUtils.getNowDate());
            insertWorks(works);
            //插入作品详情
            WorksDetail worksDetail = new WorksDetail();
            BeanUtils.copyProperties(worksSignReq,worksDetail);
            worksDetail.setWorksId(works.getId());
            worksDetailMapper.insert(worksDetail);
            return "报名成功";
        }
       else{
           //修改作品
           Works works = worksMapper.selectWorksById(worksSignReq.getId());
           BeanUtils.copyProperties(worksSignReq,works);
           worksMapper.updateWorks(works);
           WorksDetail worksDetail = worksDetailMapper.selectByPrimaryKey(worksSignReq.getId());
           BeanUtils.copyProperties(worksSignReq,worksDetail);
           worksDetailMapper.updateByPrimaryKey(worksDetail);
            return "修改成功";
        }
    }

    @Override
    public WorksDetail selectWorksDetailById(Long id) {
        WorksDetail worksDetail = worksDetailMapper.selectByPrimaryKey(id);
        return worksDetail;
    }

    @Override
    public Works review(ReviewWorkReq reviewWorkReq) {
        Works works = worksMapper.selectWorksById(reviewWorkReq.getId());
        if(works==null){
            throw new RuntimeException("作品不存在");
        }
        works.setScore(reviewWorkReq.getScore());
        worksMapper.updateWorks(works);
        return works;
    }

}
